mac-virtualcam: Fix IOSurface memory leak #6639
Merged
+36
−8
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This change fixes a memory leak in the mac-virtualcam plugin that causes OBS to not release the CVPixelBuffers (and underlying IOSurfaces) it emits to the virtual camera consumers.
Description
I have found that the plugin is leaking Mach ports associated with IOSurfaces, preventing them from being re-used. The previous approach using
NSMachPort
does not seem to properly release the Mach port allocated viaCVPixelBufferGetIOSurface
andIOSurfaceLookupFromMachPort
. Instead, we must explicitly deallocate the port usingmach_port_deallocate
.Motivation and Context
Pull request #6573 (Avoid transcoding where possible) updated the mac-virtualcam plugin to share the virtual camera feed with other processes via IOSurfaces.
Although the changes work correctly, users have observed that OBS memory usage keeps increasing when the virtual camera is active until OBS runs out of memory or the consuming application is closed.
See the report by @SciTechNick for more information: #6573 (comment)
How Has This Been Tested?
I have tested the changes on a Macbook Pro (M1) running macOS Monterey with Google Chrome, Zoom, and Cameo. OBS shows no signs of memory leakage after multiple minutes.
Types of changes
Bug fix
Checklist: